package com.gzw.dms.utils;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by gaozhiwei on 2015/9/9.
 */
public class DBHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "test.db";
    private static final int DATABASE_VERSION = 1;

    public DBHelper(Context context) {
        //CursorFactory设置为null,使用默认值
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    //数据库第一次被创建时onCreate会被调用
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE IF NOT EXISTS thq" +
                "(id int NOT NULL, mc VARCHAR, jgid INTEGER, tojgid INTEGER,pcdid INTEGER,lx VARCHAR,bz VARCHAR)");
        db.execSQL("CREATE TABLE IF NOT EXISTS tydzz" +
                "(id INTEGER primary key autoincrement, ydid INTEGER,jgid INTEGER,sljgmc VARCHAR,tojgid INTEGER," +
                "zdwzmc VARCHAR,hqid INTEGER,bh VARCHAR,bqsl INTEGER,zjs INTEGER,ydztxx INTEGER)");
        db.execSQL("CREATE TABLE IF NOT EXISTS tcyxy" +
                "(id int NOT NULL, bh VARCHAR,jgid INTEGER,jgmc VARCHAR,tojigid INTEGER," +
                "smzt INTEGER,pcdid INTEGER,ydid INTEGER,ydbh VARCHAR," +
                "sljgmc VARCHAR,zdwzmc VARCHAR,hwmc VARCHAR,zjs INTEGER,sfjs INTEGER,xyzt INTEGER)");
        db.execSQL(" CREATE TABLE IF NOT EXISTS Tydsmmx" +
                "(bh  TEXT(20) NOT NULL," +
                "pcid  INTEGER NOT NULL," +
                "xh  TEXT(3) NOT NULL," +
                "CONSTRAINT fkey0 FOREIGN KEY (pcid) REFERENCES Tydsmpc (id) ON DELETE CASCADE ON UPDATE CASCADE," +
                "UNIQUE (pcid ASC, xh ASC)" +
                ")");
        db.execSQL(" CREATE TABLE IF NOT EXISTS Tydsmpc" +
                " (id  int NOT NULL," +
                "bh  varchar(16) NOT NULL," +
                "zt  int NOT NULL," +
                "hqid  int," +
                "ydzzid  INTEGER," +
                "sljgmc  varchar(50)," +
                "zdwzmc  varchar(50)," +
                "UNIQUE (id ASC)" +
                ")");
        db.execSQL(" CREATE TABLE IF NOT EXISTS Txysmmx" +
                " ( id int NOT NULL," +
                "ydbh  varchar(16) NOT NULL," +
                "xh  varchar(16) NOT NULL" +
                ")");
        db.execSQL(" CREATE TABLE IF NOT EXISTS Txysmpc" +
                " (xyid  int NOT NULL," +
                "ydbh  varchar(16) NOT NULL," +
                "zt  int NOT NULL," +
                "sljgmc  varchar(50)," +
                "zdwzmc  varchar(50)" +
                ")");
        db.execSQL("CREATE TABLE IF NOT EXISTS Tydqs" +
                "(ydid INTEGER, ydbh VARCHAR,zjs INTEGER,smjs INTEGER,zyf INTEGER," +
                "dsk INTEGER,qssj VARCHAR,zt INTEGER,qsr VARCHAR," +
                "lx VARCHAR,hwmc VARCHAR,ydzzid INTEGER)");

        db.execSQL(" CREATE TABLE IF NOT EXISTS Tydqsmx" +
                " ( id int NOT NULL," +
                "ydbh  varchar(16) NOT NULL," +
                "xh  varchar(16) NOT NULL" +
                ")");
        db.execSQL(" CREATE TABLE IF NOT EXISTS Tyd " +
                "( id  INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
                " ydbh  varchar(26) NOT NULL," +
                "xh varchar(3) NOT NULL" +
                ")");
        db.execSQL(" CREATE TABLE IF NOT EXISTS TydBhg " +
                "(id  INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
                "barcode  varchar(20) NOT NULL" +
                ")");
    }

    //如果DATABASE_VERSION值被改为2,系统发现现有数据库版本不同,即会调用onUpgrade
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("CREATE TABLE IF NOT EXISTS thq" +
                "(id int NOT NULL, mc VARCHAR, jgid INTEGER, tojgid INTEGER,pcdid INTEGER,lx VARCHAR,bz VARCHAR)");
        db.execSQL("CREATE TABLE IF NOT EXISTS tydzz" +
                "(id INTEGER primary key autoincrement, ydid INTEGER,jgid INTEGER,sljgmc VARCHAR,tojgid INTEGER," +
                "zdwzmc VARCHAR,hqid INTEGER,bh VARCHAR,bqsl INTEGER,zjs INTEGER,ydztxx INTEGER)");
        db.execSQL("CREATE TABLE IF NOT EXISTS tcyxy" +
                "(id int NOT NULL, bh VARCHAR,jgid INTEGER,jgmc VARCHAR,tojigid INTEGER," +
                "smzt INTEGER,pcdid INTEGER,ydid INTEGER,ydbh VARCHAR," +
                "sljgmc VARCHAR,zdwzmc VARCHAR,hwmc VARCHAR,zjs INTEGER,sfjs INTEGER,xyzt INTEGER)");
        db.execSQL(" CREATE TABLE IF NOT EXISTS Tydsmmx" +
                "(bh  TEXT(20) NOT NULL," +
                "pcid  INTEGER NOT NULL," +
                "xh  TEXT(3) NOT NULL," +
                "PRIMARY KEY (bh ASC)," +
                "CONSTRAINT fkey0 FOREIGN KEY (pcid) REFERENCES Tydsmpc (id) ON DELETE CASCADE ON UPDATE CASCADE," +
                "UNIQUE (pcid ASC, xh ASC)" +
                ")");
        db.execSQL(" CREATE TABLE IF NOT EXISTS Tydsmpc" +
                " (id  int NOT NULL," +
                "bh  varchar(16) NOT NULL," +
                "zt  int NOT NULL," +
                "hqid  int," +
                "ydzzid  INTEGER," +
                "sljgmc  varchar(50)," +
                "zdwzmc  varchar(50)," +
                "UNIQUE (id ASC)" +
                ")");
        db.execSQL(" CREATE TABLE IF NOT EXISTS Txysmmx" +
                " ( id int NOT NULL," +
                "ydbh  varchar(16) NOT NULL," +
                "xh  varchar(16) NOT NULL" +
                ")");
        db.execSQL(" CREATE TABLE IF NOT EXISTS Txysmpc" +
                " (xyid  int NOT NULL," +
                "ydbh  varchar(16) NOT NULL," +
                "zt  int NOT NULL," +
                "sljgmc  varchar(50)," +
                "zdwzmc  varchar(50)" +
                ")");
        db.execSQL("CREATE TABLE IF NOT EXISTS Tydqs" +
                "(ydid INTEGER, ydbh VARCHAR,zjs INTEGER,smjs INTEGER,zyf INTEGER," +
                "dsk INTEGER,qssj VARCHAR,zt INTEGER,qsr VARCHAR," +
                "lx VARCHAR,hwmc VARCHAR,ydzzid INTEGER)");

        db.execSQL(" CREATE TABLE IF NOT EXISTS Tydqsmx" +
                " ( id int NOT NULL," +
                "ydbh  varchar(16) NOT NULL," +
                "xh  varchar(16) NOT NULL" +
                ")");
        db.execSQL(" CREATE TABLE IF NOT EXISTS Tyd" +
                "( id  INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
                " ydbh  varchar(26) NOT NULL," +
                "xh varchar(3) NOT NULL" +
                ")");
    }
}